 using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net.Sockets;
using System.Text;
using System.Threading;

namespace InternetMonitoringServer
{
    class Server
    {
        private static Hashtable clientsList;
        private static TcpListener serverSocket;
        private static TcpClient clientSocket;// = default(TcpClient);
        private static bool isServerStarted = false;

        public static string log=string.Empty;
        
        /*
         * StartGenServer Method
         * Initializes the server, start it up...etc etc...wahahaa Juniro Boy
         * */
        public static void StartServer()
        {
            clientsList = new Hashtable();

            serverSocket = new TcpListener(8888);
            clientSocket = default(TcpClient);
            

            serverSocket.Start();
            //Console.WriteLine("Server Started ....");
            log += "Server Started!\n";
            
            isServerStarted = true;

            Thread th = new Thread(doStart);
            th.Start();
            
        }

        private static void doStart()
        {
            int counter = 0;
            while (isServerStarted)
            {
                counter += 1;
                byte[] bytesFrom = new byte[10025];
                string dataFromClient = null;
                try
                {
                    clientSocket = serverSocket.AcceptTcpClient();

                    NetworkStream networkStream = clientSocket.GetStream();
                    networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                    dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
                    dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                    if (clientsList.ContainsKey(dataFromClient))
                    {
                        //Console.WriteLine(dataFromClient + " client is already opened! Client not accepted!");
                        log += dataFromClient + " client is already opened! Client not accepted!\n";
                    }
                    else
                    {
                        clientsList.Add(dataFromClient, clientSocket);

                        //Console.WriteLine(dataFromClient + " Joined JUNIOR HAWK EYE MONITORING YOW! ");
                        log += dataFromClient + " Joined\n";
                        handleClient client = new handleClient();
                        client.startClient(clientSocket, dataFromClient, clientsList);
                    }
                }
                catch (Exception e) { }

                
            }
        }

        public static void StopServer()
        {
            isServerStarted = false;
            if (clientSocket != null)
            {
                clientSocket.Close();
            }
            serverSocket.Stop();
            log += "Server Stoped!\n";
            
        }

        private class handleClient
        {
            TcpClient clientsocket;
            string clno;
            Hashtable clientslist;

            public void startClient(TcpClient inclientsocket, string clineno, Hashtable clist)
            {
                this.clientsocket = inclientsocket;
                this.clno = clineno;
                this.clientslist = clist;
                Thread ctthread = new Thread(doCom);
                ctthread.Start();
            }

            private void doCom()
            {
                int requestcount = 0;
                byte[] bytesfrom = new byte[10025];
                string datafromclient = null;
                string rcount = null;
                requestcount = 0;

                while ((true))
                {
                    try
                    {
                        requestcount = requestcount + 1;
                        NetworkStream networkstream = clientsocket.GetStream();
                        networkstream.Read(bytesfrom, 0, (int)clientsocket.ReceiveBufferSize);
                        datafromclient = System.Text.Encoding.ASCII.GetString(bytesfrom);
                        datafromclient = datafromclient.Substring(0, datafromclient.IndexOf("$"));
                        //console.writeline("from client - " + clno + " : " + datafromclient);
                        log += "from client - " + clno + " : " + datafromclient+"\n";
                        rcount = Convert.ToString(requestcount);

                    }
                    catch (Exception ex)
                    {
                        //console.writeline(ex.tostring());
                        log += clno + " " + "Disconnected from the server";
                        clientslist.Remove(clno);
                        break;
                    }
                }//end while
            }//end dochat
        }
    }
}
